#include "consumer_module.h"
#include <iostream>

namespace TakeAwayPlatform
{
    ConsumerModule::ConsumerModule(DatabaseHandler& dbHandler) : dbHandler(dbHandler) {}

    bool ConsumerModule::addAddress(int user_id, const std::string& address)
    {
        std::string sql = "INSERT INTO addresses (user_id, address) VALUES (" + std::to_string(user_id) + ", '" + address + "')";
        return dbHandler.update(sql);
    }

    bool ConsumerModule::updateAddress(int user_id, const std::string& address)
    {
        std::string sql = "UPDATE addresses SET address = '" + address + "' WHERE user_id = " + std::to_string(user_id);
        return dbHandler.update(sql);
    }

    bool ConsumerModule::deleteAddress(int user_id)
    {
        std::string sql = "DELETE FROM addresses WHERE user_id = " + std::to_string(user_id);
        return dbHandler.update(sql);
    }

    bool ConsumerModule::viewAddresses(int user_id, std::vector<std::string>& addresses)
    {
        std::string sql = "SELECT address FROM addresses WHERE user_id = " + std::to_string(user_id);
        Json::Value result = dbHandler.query(sql);

        for (const auto& row : result)
        {
            addresses.push_back(row["address"].asString());
        }

        return !addresses.empty();
    }
}